package br.ufrn.engcomp.domain;

import java.util.Random;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import br.ufrn.engcomp.arq.PersistDB;

/**
 * Representa um controlador manual (malha aberta).
 * 
 * @author Victor Carvalho Galvao de Freitas
 * 
 */
@Entity
@Table(name = "manual_controller")
public class ManualController implements PersistDB {

    public static final int STEP = 0;
    public static final int SQUARED_WAVE = 1;
    public static final int SIN = 2;
    public static final int SAW_TOOTH = 3;
    public static final int RANDOM = 4;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "manual_controller_seq")
    @SequenceGenerator(name = "manual_controller_seq", sequenceName = "id_manual_controller", allocationSize = 0)
    private int id;

    private int signalType;

    private Double amplitude;

    private Double period;

    @Column(name = "off_set")
    private Double offSet;

    public ManualController() {
    }

    public double getControlSignal(double tempo) {
	switch (signalType) {
	case (STEP):
	    return (amplitude + offSet);
	case (SIN):
	    return (amplitude * Math.sin((2 * 3.1415 * tempo) / period) + offSet);
	case (SQUARED_WAVE):
	    return (tempo % period <= period / 2 ? offSet : amplitude + offSet);
	case (SAW_TOOTH):
	    return ((amplitude * tempo) % period + offSet);
	case (RANDOM):
	    return (amplitude * new Random().nextDouble() + offSet);
	}
	return 0;
    }

    public int getSignalType() {
	return signalType;
    }

    public void setSignalType(int signalType) {
	this.signalType = signalType;
    }

    public Double getAmplitude() {
	return amplitude;
    }

    public void setAmplitude(Double amplitude) {
	this.amplitude = amplitude;
    }

    public Double getPeriod() {
	return period;
    }

    public void setPeriod(Double period) {
	this.period = period;
    }

    public Double getOffSet() {
	return offSet;
    }

    public void setOffSet(Double offSet) {
	this.offSet = offSet;
    }

    public int getId() {
	return id;
    }

    public void setId(int id) {
	this.id = id;
    }

}
